﻿Imports System.Data.OleDb

Public MustInherit Class QuestionModel
    Inherits ModelWithGUID

    Private _sequence As Integer
    Private _question As String
    Private _type As QuestionType
    Private _quizGUID As String
    Private _quiz As QuizModel

    Public Property Sequence As Integer
        Get
            Return _sequence
        End Get
        Set(ByVal value As Integer)
            If _sequence <> value Then
                _isSaved = False
                _sequence = value
            End If
        End Set
    End Property

    Public ReadOnly Property HasSequence As Boolean
        Get
            Return _sequence <> -1
        End Get
    End Property

    Public Property Question As String
        Get
            Return _question
        End Get
        Set(ByVal value As String)
            If _question <> value Then
                _isSaved = False
                _question = value
            End If
        End Set
    End Property

    Public ReadOnly Property HasQuestion As Boolean
        Get
            Return _question <> ""
        End Get
    End Property

    Public Property Type As QuestionType
        Get
            Return _type
        End Get
        Set(ByVal value As QuestionType)
            If value <> _type Then
                _isSaved = False
                _type = value
            End If
        End Set
    End Property

    Public ReadOnly Property HasType As Boolean
        Get
            Return _type <> QuestionType.None
        End Get
    End Property

    Public Property QuizGUID As String
        Get
            Return _quizGUID
        End Get
        Set(ByVal value As String)
            If value <> QuizGUID Then
                If Not IsNothing(_quiz) Then
                    _quiz = Nothing
                End If
                _isSaved = False
                _quizGUID = value
            End If
        End Set
    End Property

    Public Property Quiz As QuizModel
        Get
            If IsNothing(_quiz) AndAlso HasQuiz Then
                _quiz = New QuizModel(_quizGUID)
            End If
            Return _quiz
        End Get
        Set(ByVal value As QuizModel)
            If IsNothing(value) Then
                If QuizGUID <> "" Then
                    QuizGUID = ""
                    _quiz = Nothing
                    _isSaved = False
                End If
            Else
                If value.GUID <> QuizGUID Then
                    QuizGUID = value.GUID
                    _quiz = value
                    _isSaved = False
                End If
            End If
        End Set
    End Property

    Public ReadOnly Property HasQuiz
        Get
            Return _quizGUID <> ""
        End Get
    End Property

    Protected Sub InitialiseFromDataReader(ByVal dr As OleDbDataReader)
        GetField(_sequence, dr, "QuestionSequence", -1)
        GetField(_question, dr, "Question", "")
        GetField(_type, dr, "Type", QuestionType.None)
        GetField(_quizGUID, dr, "QuizGUID", "")
        _quiz = Nothing
    End Sub

    Protected Overrides Sub CreateBlank()
        _sequence = -1
        _question = ""
        _type = QuestionType.None
        _quiz = Nothing
    End Sub

    Public Sub New(ByVal newId As String)
        MyBase.New(newId)
    End Sub
End Class
